package com.qiguliuxing.dts.wx.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.qiguliuxing.dts.core.storage.StorageService;
import com.qiguliuxing.dts.core.util.ResponseUtil;
import com.qiguliuxing.dts.db.domain.DtsPublishRecord;
import com.qiguliuxing.dts.db.service.DtsPublishRecordService;
import com.qiguliuxing.dts.wx.annotation.LoginUser;

@RestController
@RequestMapping("/wx/publish")
@Validated
public class WxPublishController {
    @Autowired
    private DtsPublishRecordService publishService;
    @Autowired
    private StorageService storageService;

    // 创建发布记录
    @PostMapping("create")
    public Object create(@LoginUser Integer userId,
            @RequestBody DtsPublishRecord record) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        record.setUserId(userId);
        int result = publishService.createPublishRecord(record);
        return ResponseUtil.ok(result);
    }

    // 获取用户发布列表
    @GetMapping("list")
    public Object list(@LoginUser Integer userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<DtsPublishRecord> records = publishService.getUserPublishRecords(userId, page, size);
        return ResponseUtil.ok(records);
    }

    // 获取发布详情
    @GetMapping("detail")
    public Object detail(@LoginUser Integer userId,
            @RequestParam Integer id) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        DtsPublishRecord record = publishService.getPublishDetail(id);
        if (record == null || !record.getUserId().equals(userId)) {
            return ResponseUtil.fail(403, "无权限查看");
        }
        return ResponseUtil.ok(record);
    }

    // 取消发布
    @PostMapping("cancel")
    public Object cancel(@LoginUser Integer userId,
            @RequestParam Integer id) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        int result = publishService.cancelPublish(userId, id);
        return ResponseUtil.ok(result);
    }

    // 更新发布状态
    @PostMapping("updateStatus")
    public Object updateStatus(@LoginUser Integer userId,
            @RequestParam Integer id,
            @RequestParam Integer status) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        int result = publishService.updatePublishStatus(id, status);
        return ResponseUtil.ok(result);
    }

    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
                originalFilename);
        Map<String, Object> data = new HashMap<>();
        data.put("url", url);

        return ResponseUtil.ok(data);
    }

}